<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class BaseRelationBase extends Base {
	var $table = '';
	
	//table
	var $table_from = '';
	var $table_to = '';
	
	//key
	var $key_from = '';
	var $key_to = '';
	var $table_from_key = '';
	var $table_to_key = '';
	
	//one2one, one2many, many2many
	var $relation = '';
	
	//constructor
	function BaseRelationBase() {
		parent::Base();		
	}
	
	//methods
	function checkItem($from, $to, $check = 0) {
		$filter = array();
		$filter['key_from'] = $from;
		$filter['key_to'] = $to;
		
		return $this->getCountA($filter, $check);
	}
	
	function getItems($filter = '', $check = 0, $limit = true) {
		$params = "1";
		if( isset($filter['key_from']) ) {
			$params .= " and " . $this->key_from . " = '" . $filter['key_from'] . "' ";
		}
		if( isset($filter['key_to']) ) {
			$params .= " and " . $this->key_to . " = '" . $filter['key_to'] . "' ";
		}
		
		return $this->adb->getRowsA($this->table, $params, $limit);
	}
	
	function getItemsTotal($filter = '', $check = 0) {
		$params = "1";
		if( isset($filter['key_from']) ) {
			$params .= " and " . $this->key_from . " = '" . $filter['key_from'] . "' ";
		}
		if( isset($filter['key_to']) ) {
			$params .= " and " . $this->key_to . " = '" . $filter['key_to'] . "' ";
		}
		
		return $this->adb->getCountA($this->table, $params);
	}
	
	function getItemsFrom($filter = '', $check = 0, $limit = true) {
		$query = "select a.*, r.* from " . $this->table_from . " a, " . $this->table . " r " 
				. " where a." . $this->table_from_key . " = r." . $this->key_from . " ";
		$params = "1";
		if( isset($filter['key_from']) ) {
			$params .= " and r." . $this->key_from . " = '" . $filter['key_from'] . "' ";
		}
		if( isset($filter['key_to']) ) {
			$params .= " and r." . $this->key_to . " = '" . $filter['key_to'] . "' ";
		}
		$query .= $params;
		
		return $this->adb->getRows($query, $limit);
	}
	
	function getItemsTo() {
		$query = "select b.*, r.* from " . $this->table_to . " b, " . $this->table . " r " 
				. " where b." . $this->table_to_key . " = r." . $this->key_to . " ";
		$params = "1";
		if( isset($filter['key_from']) ) {
			$params .= " and r." . $this->key_from . " = '" . $filter['key_from'] . "' ";
		}
		if( isset($filter['key_to']) ) {
			$params .= " and r." . $this->key_to . " = '" . $filter['key_to'] . "' ";
		}
		$query .= $params;
		
		return $this->adb->getRows($query, $limit);
	}
	
	function getItemsFromTo() {
		$query = "select a.*, b.*, r.* from " . $this->table_from . " a, " . $this->table_to . " b, " . $this->table . " r " 
				. " where a." . $this->table_from_key . " = r." . $this->key_from . " and b." . $this->table_to_key . " = r." . $this->key_to . " ";
		$params = "1";
		if( isset($filter['key_from']) ) {
			$params .= " and r." . $this->key_from . " = '" . $filter['key_from'] . "' ";
		}
		if( isset($filter['key_to']) ) {
			$params .= " and r." . $this->key_to . " = '" . $filter['key_to'] . "' ";
		}
		$query .= $params;
		
		return $this->adb->getRows($query, $limit);
	}
	
	//
	function addItem($from, $to) {
		$data_array = array($this->key_from => $from, $this->key_to => $to);
		
		return $this->adb->insertA($this->table, $data_array);
	}
	
	function deleteItem($from, $to) {
		$query = "delete from " . $this->table . " " 
				. $this->key_from . " = '" . $from . "' and " . $this->key_to . " = '" . $to . "' " 
				. " limit 1";
		
		return $this->adb->delete($query);
	}
}
//
?>